/*
 * Copyright (C) 2012 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.io.gesture;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import ch.bfh.aeneas.navigation.around.AroundMe;
import ch.bfh.aeneas.navigation.find.FindMe;
import ch.bfh.aeneas.navigation.route.RouteMe;
import ch.bfh.aeneas.navigation.track.TrackMe;
import ch.bfh.aeneas.utils.export.ExportMe;
import java.util.ArrayList;

/**
 * This Activity offers basic gesture handling. 
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class GestureActivity extends Activity implements OnGesturePerformedListener {

    private static final String LOG_TAG = "GestureActivity";
    
    //Gestureoverlay beginn
    private View mChildView;
    private GestureLibrary mGestureLib;
    private Resources res;
    //private String[] mlistItems;
    private String[] mlistItems = {"0", "1", "2", "3", "4","5"};
    //Gestureoverlay end
    
    public void onCreate(Bundle icicle, int resId) {
        super.onCreate(icicle);
        
        boolean useGestures = true;
        
        Intent startIntent = super.getIntent();
        if(startIntent.hasExtra("useGestures")) {
            useGestures = startIntent.getBooleanExtra("useGestures", useGestures);
        }
        
        mGestureLib = GestureLibraries.fromFile("/mnt/sdcard/aeneas/gestures");
	if (useGestures && mGestureLib.load()) {
            mChildView = getLayoutInflater().inflate(resId, null);
            GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
            gestureOverlayView.addView(mChildView);
            gestureOverlayView.addOnGesturePerformedListener(this);
            this.setContentView(gestureOverlayView);
        } else {
            this.setContentView(resId);
        }        
    }
    
    /**
     * Gets called, when a gesture was drawn.
     * @param overlay
     * @param gesture 
     */
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);
        // We want at least one prediction
        
        Prediction best = null;
        Prediction p;
        for(int i = 0; i < predictions.size(); i++) {
            p = predictions.get(i);
            if(best == null || p.score > best.score) {
                best = p;
            }
        }
        
        if (best != null && best.score > 1.0) {
            String action = predictions.get(0).name;
            Log.i(LOG_TAG, "Found Action :"+action );
            if (mlistItems[0].equals(action)) {
                Intent i = new Intent(this,GestureBuilder.class);
                this.startActivity(i);
            } else if (mlistItems[1].equals(action)) {                
                Intent i = new Intent(this,AroundMe.class);
                this.startActivity(i);
            } else if (mlistItems[2].equals(action)) {                
                Intent i = new Intent(this,FindMe.class);
                this.startActivity(i);
            } else if (mlistItems[3].equals(action)) {
                Intent i = new Intent(this,RouteMe.class);
                this.startActivity(i);
            } else if (mlistItems[4].equals(action)) {
                Intent i = new Intent(this,TrackMe.class);
                this.startActivity(i);
            }else if (mlistItems[5].equals(action)) {               
                Intent i = new Intent(this,ExportMe.class);
                this.startActivity(i);
            }
        }
    }
}
